﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:DatePicker runat=server></{0}:DatePicker>")]
    public class DatePicker : TextBox
    {

        public string DateFormat
        {
            get
            {
                object obj = ViewState["DateFormat"];
                if (obj == null) { return System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToLower().Replace("yyyy", "yy"); }
                return obj.ToString();
            }
            set { ViewState["DateFormat"] = value; }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            writer.AddAttribute("type", "text/javascript");
            writer.AddAttribute("language", "javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.Write(string.Format("$(\"#{0}\").datepicker(", this.ClientID));
            if (!string.IsNullOrEmpty(DateFormat))
            {
                writer.Write("{dateFormat: '" + DateFormat + "'}");
            }

            writer.WriteLine(");");
            writer.RenderEndTag();
        }
    }
}
